<template>
  <div>
    <el-button
        type="primary"
        icon="el-icon-edit"
        @click="dialogVisible = true"
        size="mini"
    ></el-button>
    <!-- 编辑用户对话框 -->
    <el-dialog
        title="编辑公告信息"
        :visible.sync="dialogVisible"
        width="50%"
        :destroy-on-close="destroyonclose"
        @open="getNewsInfo"
    >
      <el-form
          :status-icon="status_icon"
          label-position="right"
          :model="news" :rules="rules" ref="news" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="news.title" maxlength=50 show-word-limit placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
              type="textarea"
              :autosize="{ minRows: 5}"
              maxlength=255
              placeholder="请输入公告内容"
              v-model="news.content"
              show-word-limit>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('news')">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "EditNews",
  props: {
    newsId: String
  },
  data() {
    return {
      status_icon: true,
      dialogVisible: false,
      destroyonclose: true,
      news: {
        title:'',
        content: ''
      },
      rules: {
        title: {required: true, min: 2, max: 50, message: '请输入公告名称,长度在2到50个字符', trigger: 'blur'},
        content: {required: true, min: 2, max: 255, message: '请输入分类名称,长度在2到255个字符', trigger: 'blur'}
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post('/admin/news/update', this.news)
              .then((res) => {
                if (res.data.code == 1) {
                  this.$notify.success({
                    title: "修改成功",
                    message: "已成功保存了一条公告信息！"
                  })
                  this.dialogVisible = false
                  this.$emit("editSuccess")
                } else {
                  this.$message.error(res.data.msg)
                }
              })
        } else {
          this.$message.error('请检查所填写的公告信息！')
          return false;
        }
      });
    },
    async getNewsInfo() {
      this.axios.get("/admin/news/get/" + this.newsId)
          .then(res => {
            if (res.data.code == 1){
              this.news = res.data.data
            }else{
              this.$message.error("获取公告信息失败！")
            }
          })
    }
  }
}
</script>

<style scoped>

</style>